<link href="<?php echo base_url(); ?>public/admin/css/threads.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<?php echo base_url(); ?>public/admin/assets/js/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>public/admin/script/jquery-ui.js"></script>
<div id="threads" class="">	
		<p class="heading" style="text-align: center; font-weight: bold">BÀI VIẾT</p>
		<table class="data">
			<tbody>
				<tr class="header">
					<th width="16%"><a href="<?php echo $backend;?><?php if($sort_by_date == ""){ echo "Date_asc"; } else { echo $sort_by_date; } ?>">Ngày</a></th>
					<th width="28%">Tiêu đề</th>
					<th width="14%"><a href="<?php echo $backend;?><?php if($sort_by_loca == ""){ echo "Loca_asc"; } else { echo $sort_by_loca; } ?>">Vị trí</a></th>
					<th width="10%"><a href="<?php echo $backend;?><?php if($sort_by_user == ""){ echo "User_asc"; } else { echo $sort_by_user; } ?>">Người đăng</a></th>
					<th width="8%"><a href="<?php echo $backend;?><?php if($sort_by_status == ""){ echo "Status_asc"; } else { echo $sort_by_status; } ?>">Trạng thái</a></th>
					<th width="8%"><a href="<?php echo $backend;?><?php if($sort_by_pri == ""){ echo "Priority_asc"; } else { echo $sort_by_pri; } ?>">Ưu tiên</a></th>
					<th width="14%"></th>
				</tr>						
                	<tbody>
                		<?php 
                			$page = 1;
							$perPage = 10;

							if (!empty($_GET['page'])) {
							    $page = $_GET['page']; // using the get method
							} else {
							    $page = 1; // if we don't have a page number then assume we are on the first page
							}

							$length = count($news);
							$pages = ceil($length / $perPage);
							$start  = ceil(($page - 1) * $perPage);

							$threads = array_slice($news, $start, $perPage);

							echo "<p style='text-align:center;'>";

							// first/prev pagination hyperlinks
							if ($page == 1) {
							   // echo " Đầu Trước ";
							} else {
							   echo " <a href='?page=1'>Đầu</a> ";
							   $prevpage = $page-1;
							   echo " <a href='?page=$prevpage'>Trước</a> ";
							}

							// Display current page or pages
							echo " ( Trang $page trên $pages ) ";

							// next/last pagination hyperlinks
							if ($page == $pages) {
							   // echo " Tiếp Cuối ";
							} else {
							   $nextpage = $page+1;
							   echo " <a href='?page=$nextpage'>Tiếp</a> ";
							   echo " <a href='?page=$pages'>Cuối</a> ";
							}

							echo "</p>";
                		 ?>
        			<?php foreach ($threads as $a):?>
        			<tr class="data odd-false selected-false" style="border-bottom: dashed thin #cccccc">
        				<td><?php 

        					$date = $a['DateCreated'];

        					$y = substr($date, 0, 4);
							$m = substr($date, 5, 2);
							$d = substr($date, 8, 2);
							echo $d."/".$m."/".$y." lúc ";

							$h = substr($date, 11, 2);
							$m = substr($date, 14, 2);
							$s = substr($date, 17, 2);
							echo $h.":".$m.":".$s;

        				?></td>
        				<td><?php echo $a['Title'];?></td>
        				<td>
        					<?php 
        						foreach($loca as $l){
		                            if($l['Id'] == $a['LocationId']){
		                                echo $l['LocationName'];
		                            }
                            	}
                            ?>
                        </td>
        				<td>
							<?php 
        						foreach($user as $u){
		                            if($u['UserId'] == $a['UserId']){
		                                echo $u['UserName'];
		                            }
                            	}
                            ?>
        				</td>
        				<td>
        					<?php 
		        				$status = "";
								if ($a['status'] == "true") {
									$status = "Hiện";
								}
								else{
									$status = "Ẩn";
								}
								echo $status;
							?>
						</td>	
        				<td><?php echo $a['Priority'];?></td>
        				<td style="border-left: solid thin #cccccc">
								<div class="btn-group">
									<a class='btn btn-inverse btn-small' id="<?php echo $a['Id']?>" onclick="openPopup('#popupCategory<?php echo $a['Id']?>');"><i class="icon-book"></i> Chi tiết</a>
								   	<a class="btn btn-inverse btn-small dropdown-toggle" data-toggle="dropdown" href=""><span class="caret"></span></a>
								  	<ul class="dropdown-menu">
									    <li><a href="<?php echo $backend.'edit_threads/'.$a['Id']?>"><i class="icon-edit"></i> Sửa</a></li>
									    <li><a href="<?php echo $backend.'delete_threads/'.$a['Id']?>" onclick="return checkMe()"><i class="icon-trash"></i> Xóa</a></li>
							    	</ul>
								</div>	
						</td>						
				    </tr>
				<div id="popupCategory<?php echo $a['Id']?>" class="popup-background" style="display: none">
					<div class="popup-form" style="width: 80%">
						<div class="popup-btn-close" onclick="closePopup('#popupCategory<?php echo $a['Id']?>')">X</div>
						<div class="popup-desc">
							<span style="font-weight: bold; font-size: 1.5em"><?php echo $a['Title']; ?></span>
							&nbsp; ở <?php echo $a['Address']; ?> 
								  -	<?php 
		        						foreach($loca as $l){
				                            if($l['Id'] == $a['LocationId']){
				                                echo $l['LocationName'];
				                            }
		                            	}
		                            ?>
							<style type="text/css">
								a .comment { display: none; }
								a:hover .replies { display: none; }
								a:hover .comment { display: inline; }
							</style>
							<div style="text-align: right; border-top: dashed thin #cccccc">
								
							<?php 
		        				$status = "";
								if ($a['status'] == "true") {
									echo "<a class='btn btn-primary btn-small' id='disable_threads_".$a['Id']."'". ">";
									echo "<span class='replies'><i class='icon-ok'></i>&nbsp;Hiện&nbsp;</span>";
									echo "<span class='comment'><i class='icon-remove'></i>&nbsp;&nbsp;Ẩn&nbsp;&nbsp;&nbsp;</span></a>";
								}
								else{
									echo "<a class='btn btn-primary btn-small' id='enable_threads_".$a['Id']."'". ">";
									echo "<span class='replies'><i class='icon-remove'></i>&nbsp;&nbsp;Ẩn&nbsp;&nbsp;</span>";
									echo "<span class='comment'><i class='icon-ok'></i>&nbsp;Hiện&nbsp;</span></a>";
								}
								echo $status;
							?>
							</div>
						</div> <div class="popup-desc">đăng bởi  
							<span style="font-style: italic;"><?php 
								foreach($user as $u){
		                            if($u['UserId'] == $a['UserId']){
		                                echo $u['UserName'];
		                            }
                            	} 
                        	?></span>
                        	vào 
							<?php 
								$date = $a['DateCreated'];

	        					$y = substr($date, 0, 4);
								$m = substr($date, 5, 2);
								$d = substr($date, 8, 2);
								echo $d."/".$m."/".$y." lúc ";

								$h = substr($date, 11, 2);
								$m = substr($date, 14, 2);
								$s = substr($date, 17, 2);
								echo $h.":".$m.":".$s;
							 ?> &nbsp;&nbsp;
							<span style="font-size: 1.2em">Tọa độ:</span> <?php echo $a['Latitude']; ?>
							 - <?php echo $a['Longitude']; ?>
							&nbsp;&nbsp; <span style="font-size: 1.2em">Độ ưu tiên:</span> <?php echo $a['Priority']; ?>
							&nbsp;&nbsp; <span style="font-size: 1.2em">Số người xem:</span> <?php echo $a['watch']; ?>							 
							&nbsp;&nbsp; <span style="font-size: 1.2em">Ngày cập nhập:</span> 
							<?php 
								$modify = $a['modify'];

	        					$y1 = substr($modify, 0, 4);
								$m1 = substr($modify, 5, 2);
								$d1 = substr($modify, 8, 2);
								echo $d1."/".$m1."/".$y1." lúc ";

								$h1 = substr($modify, 11, 2);
								$m1 = substr($modify, 14, 2);
								$s1 = substr($modify, 17, 2);
								echo $h1.":".$m1.":".$s1;
							?>
						</div>
						<div class="popup-content">
							<div class="popup-data">								
								<span style="font-style: italic;">Tóm tắt: </span><?php echo $a['Summary']; ?><br>
								<?php foreach($a['PlaceImages'] as $img) { echo "<img src='".$img."' width='300px' height='300px'>"; } ?>
								<p><?php echo $a['Description']; ?></p>
							</div>
							<div class="popup-control">								
				               	<a class='btn btn-warning btn-small' id='edit_threads_<?php echo $a['Id']; ?>'><i class='icon-edit'></i> Sửa</a>
				                <a class='btn btn-danger btn-small' id='delete_threads_<?php echo $a['Id']; ?>'><i class='icon-trash'></i> Xóa</a>
							</div>
						</div>
						
					</div>
				</div>
							<script>
								$("#enable_threads_<?php echo $a['Id']; ?>").click(function(){
									/* Act on the event */
									window.location.href =  "<?php echo $backend ?>enable_threads/<?php echo $a['Id']; ?>"
								});
								$("#disable_threads_<?php echo $a['Id']; ?>").click(function(){
									/* Act on the event */
									window.location.href =  "<?php echo $backend ?>disable_threads/<?php echo $a['Id']; ?>"
								});
								$("#edit_threads_<?php echo $a['Id']; ?>").click(function(){
									/* Act on the event */
									window.location.href =  "<?php echo $backend ?>edit_threads/<?php echo $a['Id']; ?>"
								});
								$("#delete_threads_<?php echo $a['Id']; ?>").click(function(){
									/* Act on the event */
									window.location.href =  "<?php echo $backend ?>delete_threads/<?php echo $a['Id']; ?>"
								});
							</script>
					<?php endforeach; ?>
				</tbody>
			</tbody>
		</table>
</div>
<script>

function openPopup(id) {
	$(id).fadeIn('fast', function() {
		$('.popup-form').draggable({
			handle : ".popup-desc"
		});

	});

	$(id).click(function() {
		// Don't allow this feature for specific popup
		if (id == '#popupKitchenBill') {
			return;
		}
		closePopup(id);
	});

	$(document).keydown(function(e) {
		if (e.which == 27) {
			closePopup(id);
		}
	});

	$(id).children().each(function() {

		if ($(this).children().length > 0) {
			$(this).bind("click", function(event) {
				event.stopPropagation();
				return false;
			});
		}
	});

};

function closePopup(id) {
	$(id).fadeOut('fast');
	$(document).unbind('keydown');
};

function toggleComp(text1, text2, linkId, div) {
	$(linkId).text(($(linkId).text() == text1) ? text2 : text1);
	$(div).slideToggle(200);
}

function checkMe() {
    if (confirm("Bạn có chắc xóa không?")) {
        return true;
    } else {
        return false;
    }
}
</script>